var Layer = require('../layer');
/**
 * UDialog组件
 * @class UDialog
 * @extends Basic
 * 示例:
 *
 *     @example
 *     UDialog.alert({title:'alert',content:'test'});
 *     UDialog.confirm({title:'alert',content:'test',confirm:function(){console.log(ddd)}});
 * */
window.UDialog = (function(){
    var msgArea = document.createElement('div');
    document.body.appendChild(msgArea);

    var ExtendLayer = React.createClass({
        componentDidMount:function(){
            this.refs.layer.layerOpen();
        },
        close:function(){
            this.refs.layer.layerClose();
            this.props.closeBack && this.props.closeBack();
        },
        confirm:function(){
            this.refs.layer.layerClose();
            this.props.confirmBack();
        },
        cancel:function(){
            this.refs.layer.layerClose();
            this.props.cancelBack();
        },
        render:function(){
            return (
                <Layer ref="layer" title={<div className="ucs-alert-title"><b></b>{this.props.title}<b></b></div>} move={false} closeBack={this.close.bind(this)} confirm={this.props.confirm}
                       cancel={this.props.cancel} confirmBack={this.confirm.bind(this)} cancelBack={this.cancel.bind(this)} autoClose={this.props.autoClose} autoCloseBack={this.props.autoCloseBack}>
                    {this.props.content}
                </Layer>
            )
        }
    });
    return {
        alert:function(param){
            ReactDOM.render(<ExtendLayer title={param.title} content={param.content} closeBack={param.closeBack}/>,msgArea);
        },
        toast:function (param) {
            ReactDOM.render(<ExtendLayer title={param.title} content={param.content} autoClose={param.autoClose?param.autoClose:0} autoCloseBack={param.closeBack} closeBack={param.closeBack}/>,msgArea);
        },
        confirm:function(param){
            ReactDOM.render(<ExtendLayer title={param.title} content={param.content} confirm={param.confirmText ? param.confirmText : "确认"} cancel={param.cancelText ? param.cancelText : "取消"} confirmBack={param.confirm} cancelBack={param.cancel} closeBack={param.closeBack}/>,msgArea);
        }
    }
})();

module.exports = UDialog